import requests
from openpyxl import load_workbook
# 导出FSC检查数据，写入指定路径
url = 'http://cmp.msa.gov.cn/psc2/inspection/export?search_GTE_formAInspDate=01.10.2024&search_LTE_formAInspDate=31.12.2024&search_EQ_formAInspPortCode=&search_EQ_formAInspType=&submitInterval=0&search_EQ_formAInspDetention=N&search_EQ_formAIsRemoteInsp=&sortName=formAInspDate&sortType=DESC'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Content-Type': 'application/vnd.ms-excel;charset=UTF-8'
}
cookies = {
    # OAMAuthnCookie_cmp.msa.gov.cn:80 参数用于登录认证
    'OAMAuthnCookie_cmp.msa.gov.cn:80': 'IDNezJMx0Z2JWvf8pCVsnXsn2GdiWmppV8MufVTCpWpMI0eMuBE9grEY14XqqCgA2SgcJDn5YVnd6H8u8RVWRWM85ufwxgXTAY2ABF3hijuyD7Y0IRHqVdOo8EeUFEze7TRam963elKyhZsJGUjAiv3J8SXktUM%2FRU22uzbezU0i%2FWRR2dplrBhPVavAcN52tM%2BAJwUYD4rdpV9pXm5Fqp5Q3e4eTa7PTaE4zL%2BfDXL461LTIFZmVZzWw%2BzJY2MKHwx0fL5GiFUf5KXjWJnpYXfkc57pcg0JoKgsK04VgczOJIVV3fC42II684hO5u85Hl81PWy3lCq2j7duzyGicB1toznV2pVtDwlJvxZhfT5W1cjNeWHSFcVCDlVgMn8Z%2BMBS5kAAIuLkw6PfQcvKU%2BXaFHlTaBreqITW7KPffiSFjvcthbGABtTs5Zph5NgBy%2BBXIuxpqcpzD1Hdhm1NedXvqOxOlyqYzzGN%2FOnGnJw%3D',
    # OAMAuthnHintCookie和JSESSIONID 参数用于声明返回接口数据，不配置的会一直返回HTML
    'OAMAuthnHintCookie': '1',
    'JSESSIONID': '3CCEDD9E28BA32E6A829259B1C5FE9A5'
}

session = requests.Session()
response_lo = session.get(url, headers=headers, cookies=cookies)
# D:\File
if response_lo.status_code == 200:
    print(response_lo.content)
    with open('D:\FscData\downloaded_file.xlsx', 'wb') as f:
        f.write(response_lo.content)
else:
    print(2)